package com.wowangz.cms.syscore.autotask.job;

import java.util.HashMap;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.dailyoperation.storechannel.pojo.StoreChannel;
import com.wowangz.cms.dailyoperation.storechannel.service.StoreChannelService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.autotask.pojo.MQQueueData;
import com.wowangz.cms.syscore.autotask.service.StoreDatabaseResourceService;
import com.wowangz.cms.syscore.pojo.RouteInfo;
import com.wowangz.cms.syscore.utils.DataPageUtil;

public class CurDataToHisDataJob extends BaseJob{

	/**
	 * 日志对象
	 */
	private static final Logger		logger					= LoggerFactory.getLogger(CurDataToHisDataJob.class);
	
	@Override
	public void innerExecute(JobExecutionContext context) throws Exception
	{

		logger.info("当日数据转历史数据自动任务开始运行");
		try
		{
			HashMap<String,Object> query = new HashMap<String,Object>();
			query.put("state",SysConstant.CommonApproveStatus.OK);
			query.put("statusid",SysConstant.RecordStatus.VALID);
			StoreDatabaseResourceService storeDatabaseResourceService = (StoreDatabaseResourceService) IssTransactionContainer.getInstance(StoreDatabaseResourceService.class, false);
			long count=storeDatabaseResourceService.queryStoreDatabaseResourceCount(query);
			int pageStartRow=0,pageEndRow=0;
			int page=DataPageUtil.totalPages(Long.valueOf(count).intValue());
			for (int i = 1; i < page+1; i++) {
				pageStartRow=DataPageUtil.pageNum *(i-1);
				pageEndRow=DataPageUtil.pageNum;
				query.put("startrow",pageStartRow);
				query.put("endrow",pageEndRow);
				List<RouteInfo> list = storeDatabaseResourceService.queryStoreDatabaseResource(query);
				MQQueueData data = null;
				StoreChannelService storeChannelService = (StoreChannelService) IssTransactionContainer.getInstance(StoreChannelService.class, false);
				Gson json = new Gson();
				for (RouteInfo routeInfo : list)
				{
					//通过商户ID 查询加密密钥
					StoreChannel storeChannel = storeChannelService.findChannelByStoreId(routeInfo.getStoreid().toString());
					if (storeChannel == null || storeChannel.getPrivatekey() == null || storeChannel.getPublickey() == null || storeChannel.getSecretkey() == null)
					{
						logger.info("商户id：" + routeInfo.getStoreid() + "通道信息不全");
						continue;
					}
					
					data = new MQQueueData();
					data.setStoreid(routeInfo.getStoreid());
					data.setDbip(routeInfo.getIp());
					data.setDbname(routeInfo.getDbname());
					data.setDbpassword(routeInfo.getPassword());
					data.setDbusername(routeInfo.getUsername());
					data.setDbport(routeInfo.getPort());
					data.setDeskey(storeChannel.getSecretkey());
					data.setPublickey(storeChannel.getPublickey());
					data.setPrivatekey(storeChannel.getPrivatekey());
					data.setUrl(storeChannel.getUrl());
					this.sendTransDataToMq(json.toJson(data),SysConstant.MQQUEUE.MQ_BANKCURTOHIS);
				}
			}
			logger.info("当日数据转历史数据自动任务结束运行");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}


}
